package com.jason.cloud.engine.share.driver.role.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jason.cloud.engine.share.driver.enterprise.bean.EnterpriseBean;
import com.jason.cloud.engine.share.driver.enterprise.service.EnterpriseService;
import com.jason.cloud.engine.share.driver.enterprise.util.EnterpriseUtil;
import com.jason.cloud.engine.share.driver.grant.bean.GrantBean;
import com.jason.cloud.engine.share.driver.grant.service.GrantService;
import com.jason.cloud.engine.share.driver.pack.bean.PackBean;
import com.jason.cloud.engine.share.driver.role.bean.RoleBean;
import com.jason.cloud.engine.share.driver.role.service.RoleService;
import com.jason.cloud.engine.share.driver.user.bean.UserBean;
import com.jason.cloud.engine.share.driver.user.util.UserUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

@Controller
@RequestMapping("/role")
public class RoleController {

    private static final Logger logger = LoggerFactory.getLogger(RoleController.class);

    @Resource
    private RoleService roleService;

    @Resource
    private EnterpriseService enterpriseService;

    @Resource
    private GrantService grantService;

    /**
     * 添加
     * @return
     */
    @RequestMapping(value = "/copy", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public @ResponseBody JSONObject copyRole() {
        JSONObject jsonObject = new JSONObject();
        RoleBean roleBean = new RoleBean();
        roleBean.setEnterpriseId("123");
        this.roleService.copyEnterpriseRoleByDefault("d994f31b-d02f-11e7-b30d-00163e0a5ef1");
        jsonObject.put("status", "添加成功");
        return jsonObject;
    }

    /**
     * 加载
     * @param id
     * @return
     */
    @RequestMapping(value = "/query/{id}", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    public @ResponseBody JSONObject queryRoleById(@PathVariable String id) {

        JSONArray jsonArray1 = new JSONArray();

        JSONObject jsonObject1 = new JSONObject();

        //根据用户传入的ID查询当前用户是否为运维人员
        RoleBean roleBean = this.roleService.queryRoleById(id);

        /**
         * 判断查询的ROLE表中字段isOperation是否为True
         */
        if (roleBean.isOperation()) {
            jsonObject1.put("status", "200");
            jsonObject1.put("msg", "");

            JSONArray jsonArray2 = new JSONArray();
            JSONObject jsonObject2 = new JSONObject();
            jsonObject2.put("name", "资费管理");
            jsonObject2.put("target", "/cloud/console/pack/pack.jsp");
            jsonArray2.add(jsonObject2);

            JSONObject jsonObject3 = new JSONObject();
            jsonObject3.put("name", "企业管理");
            jsonObject3.put("target", "/cloud/console/enterprise/enterprise.jsp");
            jsonArray2.add(jsonObject3);

            JSONObject jsonObject4 = new JSONObject();
            jsonObject4.put("name", "订单管理");
            jsonObject4.put("target", "/cloud/console/order/order.jsp");
            jsonArray2.add(jsonObject4);

            JSONObject jsonObject5 = new JSONObject();
            jsonObject5.put("name", "业务配置");
            jsonObject5.put("children", jsonArray2);

            jsonArray1.add(jsonObject5);

            JSONObject jsonObject6 = new JSONObject();
            JSONArray jsonArray3 = new JSONArray();

            jsonObject6.put("name", "邮件参数");
            jsonObject6.put("target", "/cloud/console/email/email.jsp");
            jsonArray3.add(jsonObject6);

            JSONObject jsonObject7 = new JSONObject();
            jsonObject7.put("name", "系统配置");
            jsonObject7.put("children", jsonArray3);
            jsonArray1.add(jsonObject7);
        } else {
            //加载授权
            GrantBean grantBean = this.grantService.queryGrantByRoleId(roleBean.getId().trim());

            //返回值
            jsonObject1.put("status", "200");
            jsonObject1.put("msg", "");

            //组织管理
            if (grantBean.isGrantEnterprise() || grantBean.isGrantStaff() || grantBean.isGrantDepartment() || grantBean.isGrantRole()) {
                JSONArray jsonArray2 = new JSONArray();
                if (grantBean.isGrantEnterprise()) {
                    JSONObject jsonObject2 = new JSONObject();
                    jsonObject2.put("name", "公司信息");
                    jsonObject2.put("target", "/cloud/console/enterprise/enterprise_view.jsp");
                    jsonArray2.add(jsonObject2);
                }
                if (grantBean.isGrantStaff()) {
                    JSONObject jsonObject3 = new JSONObject();
                    jsonObject3.put("name", "用户管理");
                    jsonObject3.put("target", "/cloud/console/user/user.jsp");
                    jsonArray2.add(jsonObject3);
                }
                if (grantBean.isGrantDepartment()) {
                    JSONObject jsonObject4 = new JSONObject();
                    jsonObject4.put("name", "部门管理");
                    jsonObject4.put("target", "/cloud/console/department/department.jsp");
                    jsonArray2.add(jsonObject4);
                }
                if (grantBean.isGrantRole()) {
                    JSONObject jsonObject5 = new JSONObject();
                    jsonObject5.put("name", "权限管理");
                    jsonObject5.put("target", "/cloud/console/role/role.jsp");
                    jsonArray2.add(jsonObject5);
                }
                JSONObject jsonObject6 = new JSONObject();
                jsonObject6.put("name", "组织管理");
                jsonObject6.put("children", jsonArray2);
                jsonArray1.add(jsonObject6);
            }

            //项目管理
            if (grantBean.isGrantProject() || grantBean.isGrantFeature() || grantBean.isGrantVersion()) {
                JSONArray jsonArray3 = new JSONArray();
                if (grantBean.isGrantProject()) {
                    JSONObject jsonObject7 = new JSONObject();
                    jsonObject7.put("name", "项目管理");
                    jsonObject7.put("target", "/cloud/console/project/project.jsp");
                    jsonArray3.add(jsonObject7);
                }
                if (grantBean.isGrantVersion()) {
                    JSONObject jsonObject8 = new JSONObject();
                    jsonObject8.put("name", "版本管理");
                    jsonObject8.put("target", "/cloud/console/version/version.jsp");
                    jsonArray3.add(jsonObject8);
                }
                if (grantBean.isGrantFeature()) {
                    JSONObject jsonObject9 = new JSONObject();
                    jsonObject9.put("name", "特性管理");
                    jsonObject9.put("target", "/cloud/console/feature/feature.jsp");
                    jsonArray3.add(jsonObject9);
                }
                JSONObject jsonObject10 = new JSONObject();
                jsonObject10.put("name", "项目配置");
                jsonObject10.put("children", jsonArray3);
                jsonArray1.add(jsonObject10);
            }

            //接口测试
            if (grantBean.isGrantInterfaceCase() || grantBean.isGrantInterfaceTask()) {
                JSONArray jsonArray4 = new JSONArray();
                if (grantBean.isGrantInterfaceCase()) {
                    JSONObject jsonObject11 = new JSONObject();
                    jsonObject11.put("name", "接口用例");
                    jsonObject11.put("target", "/cloud/console/interface/inter_case.jsp");
                    jsonArray4.add(jsonObject11);
                }
                if (grantBean.isGrantInterfaceTask()) {
                    JSONObject jsonObject12 = new JSONObject();
                    jsonObject12.put("name", "测试任务");
                    jsonObject12.put("target", "/cloud/console/interface/inter_test_task.jsp");
                    jsonArray4.add(jsonObject12);
                }
                JSONObject jsonObject13 = new JSONObject();
                jsonObject13.put("name", "接口测试");
                jsonObject13.put("children", jsonArray4);
                jsonArray1.add(jsonObject13);
            }
        }

        jsonObject1.put("data", jsonArray1);
        return jsonObject1;
    }

    /**
     * 列举
     * @return
     */
    @RequestMapping(value = "/all", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    public @ResponseBody JSONObject getRoleLimitByEnterpriseId(@RequestParam int page, @RequestParam int limit, @RequestParam String enterpriseId) {

        //加载所有
        List<RoleBean> roleBeanList = this.roleService.getRoleLimitByEnterpriseId((page - 1 ) * limit, limit, enterpriseId.trim());
        int count = this.roleService.getRoleCountByEnterpriseId(enterpriseId);

        //返回值
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code", 0);
        jsonObject.put("msg", "");
        jsonObject.put("count", count);

        //遍历数据
        JSONArray jsonArrayRole = new JSONArray();
        RoleBean roleBean = null;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        for(Iterator itr = roleBeanList.iterator(); itr.hasNext();) {
            roleBean = (RoleBean)itr.next();
            JSONObject json = new JSONObject();
            json.put("id", roleBean.getId());
            json.put("name", roleBean.getName());
            json.put("describe", roleBean.getRemarks()==null?"--":roleBean.getRemarks());
            jsonArrayRole.add(json);
        }
        jsonObject.put("data", jsonArrayRole);
        return jsonObject;
    }

    /**
     * 角色添加
     * @param json
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public @ResponseBody JSONObject insertRole(@RequestBody JSONObject json) {
        JSONObject jsonObject = new JSONObject();
        String enterpriseId = json.getString("enterpriseId");
        String name = json.getString("name");
        String remarks = json.getString("remarks");

        RoleBean roleBean = new RoleBean();
        EnterpriseBean enterpriseBean = this.enterpriseService.queryEnterpriseById(enterpriseId.trim());
        if (enterpriseBean != null) {
            roleBean.setName(name);
            roleBean.setOperation(false);
            roleBean.setDefault(false);
            roleBean.setRemarks(remarks);
            roleBean.setEnterpriseId(enterpriseBean.getId().trim());
            this.roleService.insertRole(roleBean);

            jsonObject.put("status", 200);
            jsonObject.put("msg", "角色创建成功");
        } else {
            jsonObject.put("status", 201);
            jsonObject.put("msg", "角色创建失败");
        }
        return jsonObject;
    }

    /**
     * 加载
     * @return
     */
    @RequestMapping(value = "/loadRole/{enterpriseId}", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    public @ResponseBody JSONObject getRoleByEnterpriseId(@PathVariable String enterpriseId) {

        //加载所有
        List<RoleBean> roleBeanList = this.roleService.getRoleByEnterpriseId(enterpriseId.trim());

        //返回值
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code", 200);
        jsonObject.put("msg", "");

        //遍历数据
        JSONArray jsonArrayRole = new JSONArray();
        RoleBean roleBean = null;
        for(Iterator itr = roleBeanList.iterator(); itr.hasNext();) {
            roleBean = (RoleBean)itr.next();
            JSONObject json = new JSONObject();
            json.put("id", roleBean.getId());
            json.put("name", roleBean.getName());
            jsonArrayRole.add(json);
        }
        jsonObject.put("data", jsonArrayRole);
        return jsonObject;
    }
}
